Jquery / UI / Drag and Drop- Sorting
Drag and Drop: Sorting
-
1. Item wise sorting
1. library
2. HTML : direct items
Note that class="todo-list" 3. script
$('.todo-list').sortable({ placeholder : 'sort-highlight', handle : '.handle', forcePlaceholderSize: true, zIndex : 999999 }) -
-
2. Group wise sorting
1. Create group container
2. Add child items
- Item 1
3. Add script
Complete code$('.connectedSortable').sortable({ placeholder : 'sort-highlight', connectWith : '.connectedSortable', handle : '.card-header, .nav-tabs', forcePlaceholderSize: true, zIndex : 999999 }) $('.connectedSortable .card-header, .connectedSortable .nav-tabs-custom').css('cursor', 'move') - Item 1
- Item 2
- Item 1
- Item 2
$('.connectedSortable').sortable({ placeholder : 'sort-highlight', connectWith : '.connectedSortable', handle : '.card-header, .nav-tabs', forcePlaceholderSize: true, zIndex : 999999 }) $('.connectedSortable .card-header, .connectedSortable .nav-tabs-custom').css('cursor', 'move') $('.todo-list').sortable({ placeholder : 'sort-highlight', handle : '.handle', forcePlaceholderSize: true, zIndex : 999999 }) -
3. Events
- Item 1
- Item 2
$('.todo-list').sortable({ placeholder : 'sort-highlight', handle : '.handle', forcePlaceholderSize: true, zIndex : 999999, start: function(e, ui) { // creates a temporary attribute on the element with the old index $(this).attr('data-previndex', ui.item.index()); }, update: function(e, ui) { // gets the new and old index then removes the temporary attribute var newIndex = ui.item.index(); var oldIndex = $(this).attr('data-previndex'); var element_id = ui.item.attr('id'); alert('id of Item moved = '+element_id+' old position = '+oldIndex+' new position = '+newIndex); $(this).removeAttr('data-previndex'); } }) -
4. LIVE EXAMPLE
........
class="category_panel" is same for each item
data-id is different$('.connectedSortable').sortable({ placeholder : 'sort-highlight', connectWith : '.connectedSortable', handle : '.card-header, .nav-tabs', forcePlaceholderSize: true, zIndex : 999999, start: function(e, ui) { // creates a temporary attribute on the element with the old index $(this).attr('data-previndex', ui.item.index()); }, update: function(e, ui) { $('.category_panel').each(function(index, obj) { //console.log($(this).attr('data-id')); var item_id=$(this).attr('data-id'); $('#frm_sorting').append(""); }) $('#frm_sorting').submit(); } }) class="item_1" data-category="1" is same for each item
data-id is different$('.todo-list').sortable({ placeholder : 'sort-highlight', handle : '.handle', forcePlaceholderSize: true, zIndex : 999999, start: function(e, ui) { // creates a temporary attribute on the element with the old index $(this).attr('data-previndex', ui.item.index()); }, update: function(e, ui) { // gets the new and old index then removes the temporary attribute var newIndex = ui.item.index(); //var oldIndex = $(this).attr('data-id'); //console.log($(this)); var element_id = ui.item.attr('data-category'); //alert(element_id); //alert('id of Item moved = '+element_id+' old position = '+oldIndex+' new position = '+newIndex); // $(this).parent().find('li').each(function(index, obj) { // console.log(obj); // }) $('.item_'+element_id).each(function(index, obj) { var item_id=$(this).attr('data-id'); $('#frm_sorting').append(""); }) $('#frm_sorting').submit(); } });